package org.ats.utils;

import java.util.List;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

/**
 * 
 * 
 * */
public class IntentHelper {
	
	public static Intent findIntent(Context mCm,String pkgName){
		Intent mIm = null;
		PackageManager mPm = mCm.getPackageManager();
		mIm = mPm.getLaunchIntentForPackage(pkgName);
		return mIm;
	}

	/**
	 * */
	public static Intent finadIntent_1(Context mCm,String pkgName){
		Intent mIm = new Intent(Intent.ACTION_MAIN);
		mIm.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		//
		PackageManager mPm = mCm.getPackageManager();
		ComponentName cmp = null;
		Intent mTm = new Intent(Intent.ACTION_MAIN);
		mTm.addCategory(Intent.CATEGORY_LAUNCHER);
		List<ResolveInfo> ra = mPm.queryIntentActivities(mTm, 0);
		for (int i = 0; i < ra.size(); i++) {
			ActivityInfo ai = ra.get(i).activityInfo;
			if (pkgName.equals(ai.applicationInfo.packageName)) {
				ComponentName c = new ComponentName(
						ai.applicationInfo.packageName, ai.name);
				cmp = c;
				break;
			}
		}
		if(cmp!=null){
			mIm.setComponent(cmp);
			return mIm;
		}else {
			return null;
		}
	}
	
	/**
	 * */
	public static Intent finadIntent_2(Context mCm,String pkgName,String action){
		Intent mIm = new Intent(Intent.ACTION_MAIN);
		mIm.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		//
		PackageManager mPm = mCm.getPackageManager();
		ComponentName cmp = null;
		Intent mTm = new Intent(action);
		List<ResolveInfo> ra = mPm.queryIntentActivities(mTm, 0);
		for (int i = 0; i < ra.size(); i++) {
			ActivityInfo ai = ra.get(i).activityInfo;
			if (pkgName.equals(ai.applicationInfo.packageName)) {
				ComponentName c = new ComponentName(
						ai.applicationInfo.packageName, ai.name);
				cmp = c;
				break;
			}
		}
		if(cmp!=null){
			mIm.setComponent(cmp);
			return mIm;
		}else {
			return null;
		}
	}
}
